package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JPanel;

import model.Actor;
import model.Counter;
import model.Field;
import model.FieldStats;
import model.Fox;
import model.Grass;
import model.Hunter;
import model.Rabbit;
import model.Simulator;
import model.Wolf;

/**
 * The class PieView extends AbstractView
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class PieView extends AbstractView
{
    private int pieWidth, pieHeight;
    Dimension size;
    private Graphics g;
    private Image fieldImage;

    /**
     * Create a new PieView component.
     * @param simulator
     */
    public PieView(Simulator simulator)
    {
    	super(simulator);
    	pieHeight = 300;
        pieWidth = 300;

    }


    /**
     * Tell the GUI manager how big we would like to be.
     * @return the new dimension
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(pieWidth, pieHeight);
    }

    /**
     * Prepare for a new round of painting. Since the component
     * may be resized, compute the scaling factor again.
     */
    public void preparePaint()
    {
        fieldImage = this.createImage(pieWidth, pieHeight);
        g = fieldImage.getGraphics();
    }
    
    /**
     * overrides the updateView method
     */
    @Override
    public void updateView()
    {
    	this.preparePaint();
    	HashMap<Class, Counter> counters = simulator.getStats().getCounters();
    	this.preparePaint();
        double total = 0.0D;
        total = simulator.getField().getWidth() * simulator.getField().getDepth();
        
        HashMap<Class, Color> colors = simulator.getColors();
        
        double curValue = 0.0D;
        int startAngle = 0;
        int sumCount = 0;
        for (Class key: counters.keySet())
        {
        	Counter count = counters.get(key);
        	startAngle = (int)(curValue * 360 / total);
        	int arcAngle = (int)(count.getCount() * 360 / total);
        	 g.setColor(colors.get(key));
             g.fillArc(0, 0, 300, 300, startAngle, arcAngle);

             curValue += count.getCount();  
        }
        //paint empty fields
        startAngle = (int)(curValue * 360 / total);
        int arcAngle = (int)((total-curValue) * 360 / total);
        g.setColor(Color.white);
        g.fillArc(0, 0, 300, 300, startAngle, arcAngle);
        
        
        this.repaint();      	
    }

    /**
     * The pie view component needs to be redisplayed. Copy the
     * internal image to screen.
     * @param g
     */
    public void paintComponent(Graphics g)
    {
    	g.drawImage(fieldImage, 0, 0, pieWidth, pieHeight, null);

    }
}